import cv2
surf = cv2.xfeatures2d.SURF_create()  # SURF
from utils.common import load_image, show_image, make_dirs  # 导入公共函数
from utils.config import exp5_surf_path, exp5_output_dir  # 导入输出路径

# 确保输出目录存在
make_dirs(exp5_output_dir)

# 加载图片并显示
image = load_image()
show_image("Original Image", image)

# 灰度化
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
show_image("Grayscale Image", gray_image)

# 初始化SURF检测器
surf = cv2.xfeatures2d.SURF_create(400)  # 400是Hessian阈值

# 检测关键点和描述符
keypoints, descriptors = surf.detectAndCompute(gray_image, None)

# 绘制关键点
image_with_keypoints = cv2.drawKeypoints(image, keypoints, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# 显示结果
show_image("SURF Keypoints", image_with_keypoints)

# 保存结果
output_path = exp5_output_dir / "surf_keypoints.jpg"
cv2.imwrite(str(output_path), image_with_keypoints)
print(f"SURF keypoints image saved to: {output_path}")